package xuegao.study.mall.admin.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.admin.dto.PmsProductAttributeParam;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.admin.common.CommonPage;
import xuegao.study.mall.admin.service.pms.PmsProductAttributeService;
import xuegao.study.mall.common.dto.PmsProductAttrInfo;
import xuegao.study.mall.common.model.pms.PmsProductAttribute;

import java.util.List;

/**
 * 商品属性管理
 */
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {

    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;

    @ApiOperation("属性列表分页查询")
    @GetMapping("/list/{cid}")
    public CommonResult<CommonPage<PmsProductAttribute>> getList(@PathVariable Long cid,
                                                           @RequestParam(value = "type") Integer type,
                                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<PmsProductAttribute> list = pmsProductAttributeService.getList(cid, type, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("根据商品分类的id获取商品属性及属性分类")
    @GetMapping(value = "/attrInfo/{productCategoryId}")
    public CommonResult<List<PmsProductAttrInfo>> getAttrInfo(@PathVariable Long productCategoryId) {
        List<PmsProductAttrInfo> productAttrInfoList = pmsProductAttributeService.getProductAttrInfo(productCategoryId);
        return CommonResult.success(productAttrInfoList);
    }

    @ApiOperation("属性或参数详情")
    @GetMapping(value = "/{id}")
    public CommonResult<PmsProductAttribute> detail(@PathVariable("id") Long id) {
        PmsProductAttribute item = pmsProductAttributeService.getItem(id);
        return CommonResult.success(item);
    }

    @ApiOperation("属性添加")
    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody PmsProductAttributeParam param) {
        int count = pmsProductAttributeService.create(param);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改商品属性信息")
    @PostMapping(value = "/update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody PmsProductAttributeParam param) {
        int count = pmsProductAttributeService.update(id, param);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("批量删除属性")
    @PostMapping
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = pmsProductAttributeService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

}
